package web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rdc.example.bean.PageBean;
import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ColumnDTO;
import com.rdc.example.bean.dto.SongDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.ColumnService;
import com.rdc.example.service.SongService;
import com.rdc.example.service.impl.ColumnServiceImpl;
import com.rdc.example.service.impl.SongServiceImpl;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.util.jar.JarOutputStream;

@WebServlet("/column/*")
@MultipartConfig
public class ColumnServlet extends BaseServlet{

    ColumnService columnService = new ColumnServiceImpl();
    SongService songService = new SongServiceImpl();

    public void getColumnListByUserId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("getColumnListByUserId...");
        //接收 当前页码 和 每页展示条数  eg: url?currentPage=1&pageSize=5
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //在session中获取userId
        HttpSession session = req.getSession();
        Integer userId=((UserDTO) session.getAttribute("userDTO")).getId();
        //调用service
        PageBean<ColumnDTO> pageBean= columnService.getColumnListByUserId(userId,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void addColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("addColumn...");
        // 接收数据
        BufferedReader br = req.getReader ();
        //json 字符串
        String params = br.readLine ();
         // 转为 ColumnDTO 对象
        ColumnDTO columnDTO = JSON.parseObject (params, ColumnDTO.class);
        //在session中获取userId
        HttpSession session = req.getSession();
        Integer userId=((UserDTO) session.getAttribute("userDTO")).getId();
        columnDTO.setUserId(userId);
        //调用service
        Result result = columnService.insertColumn(columnDTO);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void deleteColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("deleteColumn...");
        //前端获取columnId
        Integer columnId = Integer.parseInt(req.getParameter("columnId"));
        //在session中获取userId
        HttpSession session = req.getSession();
        Integer userId=((UserDTO) session.getAttribute("userDTO")).getId();
        //调用service
        Result result=columnService.deleteColumn(userId,columnId);
        //返回结果
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void getSongListByPageAndColumnIdWithNull(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("getSongListByPageAndColumnIdWithNull...");
        //接收 当前页码 和 每页展示条数
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Integer columnId = Integer.parseInt(req.getParameter("columnId"));
        //在session中获取userId
        HttpSession session = req.getSession();
        Integer userId=((UserDTO) session.getAttribute("userDTO")).getId();
        //调用service
        PageBean<SongDTO> pageBean= songService.getSongListByPageAndColumnIdWithNull(columnId,userId,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void  inColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("inColumn...");
        Integer columnId = Integer.parseInt(req.getParameter("columnId"));
        Integer songId = Integer.parseInt(req.getParameter("songId"));
        System.out.println("columnId:"+columnId+" songId:"+songId);
        Result result= columnService.inColumn(columnId,songId);
        System.out.println(result);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void  outColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("outColumn...");
        Integer columnId = Integer.parseInt(req.getParameter("columnId"));
        Integer songId = Integer.parseInt(req.getParameter("songId"));
        Result result= columnService.outColumn(songId);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void getSongListByPageAndColumnId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("getSongListByPageAndColumnId...");
        //接收 当前页码 和 每页展示条数
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Integer columnId = Integer.parseInt(req.getParameter("columnId"));
        //调用service
        PageBean<SongDTO> pageBean= songService.getSongListByPageAndColumnId(columnId,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }






}
